<template>
  <slide-view
    v-empty="!canShowDetail"
    :listenerIDs="listenerIDs"
    :noListenerIDs="noListenerIDs"
    :noListenerClass="noListenerClass"
    @side-close="hideView"
    :body-style="{ padding: 0, height: '100%' }"
  >
    <flexbox
      v-if="canShowDetail"
      v-loading="loading"
      xs-empty-icon="nopermission"
      xs-empty-text="暂无权限"
      direction="column"
      align="stretch"
      class="d-container"
    >
      <c-r-m-detail-head
        crmType="contract"
        :canEdit="canEdit"
        @handle="detailHeadHandle"
        @close="hideView"
        :detail="detailData"
        :headDetails="headDetails"
        :id="id"
      />
      <div class="examine-info">
        <examine-info
          :id="id"
          class="examine-info-border"
          examineType="crm_contract"
          :flow_id="detailData.flow_id"
        />
      </div>
      <div class="tabs">
        <el-tabs v-model="tabCurrentName" @tab-click="handleClick">
          <el-tab-pane
            v-for="(item, index) in tabnames"
            :key="index"
            :label="item.label"
            :name="item.name"
          >
          </el-tab-pane>
        </el-tabs>
      </div>
      <div class="t-loading-content" id="follow-log-content">
        <keep-alive>
          <component
            v-bind:is="tabName"
            crmType="contract"
            :canEdit="canEdit"
            :refresh="refresh"
            :detail="detailData"
            :id="id"
            @openForm="openForm"
          />
        </keep-alive>
      </div>
    </flexbox>
    <c-r-m-create-view
      v-if="isCreate"
      crm-type="contract"
      :action="{ type: 'update', id: this.id }"
      @save-success="editSaveSuccess"
      @hiden-view="isCreate = false"
    />
    <examine-create-view
      v-if="isEdit"
      :category_id="editInfo.category_id"
      :category_title="editInfo.title"
      :action="editAction"
      @save-success="createSaveSuccess"
      @hiden-view="isEdit = false"
    />
  </slide-view>
</template>

<script>
import { adminIndexAuthList } from "@/api/common";
import { crmContractRead } from "@/api/customermanagement/contract";

import SlideView from "@/components/SlideView";
import CRMDetailHead from "../components/CRMDetailHead";
import ContractFollow from "./components/ContractFollow"; // 跟进记录
import RelativeDeputystat from "../components/RelativeDeputystat"; // 待办统计
import CRMBaseInfo from "../components/CRMBaseInfo"; // 商机基本信息
import RelativeHandle from "../components/RelativeHandle"; // 相关操作
import RelativeInvoicing from "../components/RelativeInvoicing"; // 开票申请
import RelativeExpense from "../components/RelativeExpense"; // 报销
import RelativeRefund from "../components/RelativeRefund"; // 退款申请
import RelativeCost from "../components/RelativeCost"; // 成本变更
import RelativeComputed from "../components/RelativeComputed"; // 核算成本调整
import RelativeReceivable from "../components/RelativeReceivable"; // 应收款金额
import RelativeFlowlist from "../components/RelativeFlowlist";
import RelativeTeam from "../components/RelativeTeam"; // 相关团队
import RelativeProduct from "../components/RelativeProduct"; // 产品
import RelativeReturnMoney from "../components/RelativeReturnMoney"; // 相关回款
import RelativeFiles from "../components/RelativeFiles"; // 相关附件
import ExamineInfo from "@/components/Examine/ExamineInfo";
import ExamineCreateView from "@/views/OAManagement/examine/components/examineCreateView";
import CRMCreateView from "../components/CRMCreateView"; // 新建页面

import detail from "../mixins/detail";

export default {
  /** 客户管理 的 订单详情 */
  name: "contract-detail",
  components: {
    SlideView,
    CRMDetailHead,
    ContractFollow,
    RelativeDeputystat,
    CRMBaseInfo,
    RelativeHandle,
    RelativeInvoicing,
    RelativeExpense,
    RelativeRefund,
    RelativeCost,
    RelativeComputed,
    RelativeReceivable,
    RelativeTeam,
    RelativeProduct,
    RelativeReturnMoney,
    RelativeFiles,
    ExamineInfo,
    CRMCreateView,
    ExamineCreateView,
    RelativeFlowlist,
  },
  mixins: [detail],
  props: {
    // 详情信息id
    id: [String, Number],
    // 监听的dom 进行隐藏详情
    listenerIDs: {
      type: Array,
      default: () => {
        return ["crm-main-container"];
      },
    },
    // 不监听
    noListenerIDs: {
      type: Array,
      default: () => {
        return [];
      },
    },
    noListenerClass: {
      type: Array,
      default: () => {
        return ["el-table__body"];
      },
    },
  },
  data() {
    return {
      loading: false, // 展示加载loading
      crmType: "contract",
      detailData: {}, // read 详情
      headDetails: [
        { title: "合同编号", value: "" },
        { title: "客户名称", value: "" },
        { title: "下单时间", value: "" },
        { title: "负责人", value: "" },
        { title: "订单金额（元）", value: "" },
        { title: "回款金额（元）", value: "" },
        // { title: "成本价（元）", value: "" },
        // { title: "变更后成本价（元）", value: "" },
        // { title: "核算成本（元）", value: "" },
        { title: "尾款金额（元）", value: "" },
        { title: "业绩（元）", value: "" },
        // { title: "办公件费用（元）", value: "" },
        // { title: "返点费用（元）", value: "" },
        // { title: "代收代付费用（bb元）", value: "" },
        { title: "月度服务费(元)", value: "" },
        { title: "核定成本", value: "" },
        { title: "外勤提成(元)", value: "" },
        { title: "可开票金额(元)", value: "" },
        { title: "已开票金额(元)", value: "" },
        { title: "可报销金额(元)", value: "" },
        { title: "已报销金额(元)", value: "" },
        { title: "退款金额(元)", value: "" },
        { title: "应收款金额(元)", value: "" },
        { title: "", value: "" },
        { title: "", value: "" },
        { title: "", value: "" },
      ],
      tabCurrentName: "followlog",
      refresh: Number(new Date()), // 刷新
      isCreate: false, // 编辑操作
      isEdit: false, // 是否编辑
      editInfo: {},
      editAction: {
        type: "save",
      },
      roam: false,
    };
  },
  computed: {
    tabName() {
      if (this.tabCurrentName == "followlog") {
        return "contract-follow";
        // } else if (this.tabCurrentName == "deputystat") {
        //   return "relative-deputystat";
      } else if (this.tabCurrentName == "basicinfo") {
        return "c-r-m-base-info";
      } else if (this.tabCurrentName == "team") {
        return "relative-team";
      } else if (this.tabCurrentName == "contract") {
        return "relative-contract";
      } else if (this.tabCurrentName == "operationlog") {
        return "relative-handle";
      } else if (this.tabCurrentName == "invoicing") {
        return "relative-invoicing";
      } else if (this.tabCurrentName == "expense") {
        return "relative-expense";
      } else if (this.tabCurrentName == "refund") {
        return "relative-refund";
        // } else if (this.tabCurrentName == "cost") {
        //   return "relative-cost";
      } else if (this.tabCurrentName == "comCost") {
        return "relative-computed";
      } else if (this.tabCurrentName == "receivable") {
        return "relative-receivable";
      } else if (this.tabCurrentName == "flowlist") {
        return "relative-flowlist";
      } else if (this.tabCurrentName == "product") {
        return "relative-product";
      } else if (this.tabCurrentName == "returnedmoney") {
        return "relative-return-money";
      } else if (this.tabCurrentName == "file") {
        return "relative-files";
      }
      return "";
    },
    tabnames() {
      var tempsTabs = [];
      tempsTabs.push({
        label: "跟进记录",
        name: "followlog",
      });
      // if (this.crm.contract && this.crm.contract.deputystat) {
      //   tempsTabs.push({
      //     label: "待办统计",
      //     name: "deputystat"
      //   });
      // }
      if (this.crm.contract && this.crm.contract.read) {
        tempsTabs.push({
          label: "基本信息",
          name: "basicinfo",
        });
      }
      if (this.crm.product && this.crm.product.index) {
        tempsTabs.push({
          label: "产品",
          name: "product",
        });
      }
      if (this.crm.receivables && this.crm.receivables.index) {
        tempsTabs.push({
          label: "回款信息",
          name: "returnedmoney",
        });
      }
      tempsTabs.push({
        label: "相关团队",
        name: "team",
      });
      tempsTabs.push({
        label: "附件",
        name: "file",
      });
      tempsTabs.push({
        label: "操作记录",
        name: "operationlog",
      });
      tempsTabs.push({
        label: "开票申请",
        name: "invoicing",
      });
      tempsTabs.push({
        label: "报销",
        name: "expense",
      });
      //   订单板块注释
      tempsTabs.push({
        label: "退款申请",
        name: "refund",
      });
      // tempsTabs.push({
      //   label: "成本变更",
      //   name: "cost"
      // });
      tempsTabs.push({
        label: "核定成本调整",
        name: "comCost",
      });
      tempsTabs.push({
        label: "应收款调整",
        name: "receivable",
      });
      tempsTabs.push({
        label: "跟进记录",
        name: "flowlist",
      });
      return tempsTabs;
    },
  },
  mounted() {
    //   this.getDetial();
  },
  methods: {
    // 获取权限
    getAuth() {
      return new Promise((resolve, reject) => {
        adminIndexAuthList()
          .then((response) => {
            const data = response.data;
            if (data.crm.contract) {
              this.roam = data.crm.contract.fieldcommission
                ? data.crm.contract.fieldcommission
                : false;
            }

            console.log(this.roam, "this.roam");
            resolve(response);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    getDetial() {
      this.getAuth();
      this.loading = true;

      crmContractRead({
        id: this.id,
      })
        .then((res) => {
          this.detailData = res.data; // 创建回款计划的时候使用
          this.headDetails[0].value = res.data.num;
          this.headDetails[1].value = res.data.customer_id_info
            ? res.data.customer_id_info.name
            : "";
          this.headDetails[2].value =
            res.data.order_date == "0000-00-00" ? "" : res.data.order_date;
          this.headDetails[3].value = res.data.owner_user_id_info
            ? res.data.owner_user_id_info.realname
            : "";
          this.headDetails[4].value = res.data.money;
          this.headDetails[5].value = res.data.receivablesMoney
            ? res.data.receivablesMoney.lasahmMoney
            : "";
          // this.headDetails[6].value = res.data.crm_xuonxx ?
          //   res.data.crm_xuonxx :
          //   "";

          // this.headDetails[6].value = res.data.crm_pbeelz ?
          //   res.data.crm_pbeelz :
          //   "";
          this.headDetails[6].value = res.data.arrears ? res.data.arrears : "";
          //   this.headDetails[7].value = "";
          this.headDetails[7].value = res.data.performance ? res.data.performance : "";
          // this.headDetails[8].value = res.data.crm_gvzubs ?
          //   res.data.crm_gvzubs :
          //   "";
          // this.headDetails[9].value = res.data.crm_vwvucv ?
          //   res.data.crm_vwvucv :
          //   "";
          // this.headDetails[10].value = res.data.crm_ahsoqz ?
          //   res.data.crm_ahsoqz :
          //   "";

          this.headDetails[8].value = res.data.crm_month_price
            ? res.data.crm_month_price
            : "";
          this.headDetails[9].value = res.data.crm_oeseng ? res.data.crm_oeseng : "";
          this.headDetails[10].value = res.data.crm_field_price
            ? res.data.crm_field_price
            : "";

          this.headDetails[11].value = res.data.invoice_money
            ? res.data.invoice_money
            : "0";
          this.headDetails[12].value = res.data.invoice_used_money
            ? res.data.invoice_used_money
            : "0";
          this.headDetails[13].value = res.data.expence_money
            ? res.data.expence_money
            : "0";
          this.headDetails[14].value = res.data.expence_used_money
            ? res.data.expence_used_money
            : "0";
          this.headDetails[15].value = res.data.refund_money
            ? res.data.refund_money
            : "0";
          this.headDetails[16].value = res.data.ys_amount
            ? res.data.ys_amount
            : "0";
          console.log(this.roam, "this.roam");
          if (!this.roam) {
            this.headDetails[10] = {
              title: "",
              value: "",
            };
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //** 打开弹窗 */
    openForm(value) {
      this.editInfo = value;
      this.editAction = {
        type: "save",
        id: "",
        data: this.detailData,
      };
      this.isEdit = true;
    },
    createSaveSuccess() {
      this.refresh = Number(new Date());
      this.getDetial();
    },
    //** 点击关闭按钮隐藏视图 */
    hideView() {
      this.$emit("hide-view");
    },
    //** tab标签点击 */
    handleClick(tab, event) {},
    editSaveSuccess() {
      this.$emit("handle", {
        type: "save-success",
      });
      this.getDetial();
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../styles/crmdetail.scss";
</style>
